package com.oig.sys.base.service.impl;

import com.oig.sys.base.dao.ITbInnerAreaDao;
import com.oig.sys.base.entity.TbInnerArea;
import com.oig.sys.base.service.ITbInnerAreaService;

import com.oig.sys.base.vo.InnerAreaVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbInnerAreaServiceImpl implements ITbInnerAreaService {

	private final ITbInnerAreaDao tbInnerAreaDao;

	@Autowired
	public TbInnerAreaServiceImpl(ITbInnerAreaDao tbInnerAreaDao){
		 this.tbInnerAreaDao = tbInnerAreaDao;
	}

	@Override
	public List<InnerAreaVo> findTopInnerArea(String searchWord) {
		if (searchWord==null){
			searchWord = "";
		}
		log.debug("query top ten inner area searchWord={}", searchWord);
		List<TbInnerArea> innerAreaList = this.tbInnerAreaDao.findTopTen(searchWord, PageRequest.of(0, 10));
		List<InnerAreaVo> voList = new ArrayList<>();
		if(innerAreaList != null && innerAreaList.size() > 0){
			innerAreaList.forEach(innerArea ->{
				InnerAreaVo vo = new InnerAreaVo();
				vo.setCode(innerArea.getCode());
				vo.setName(innerArea.getName());
				voList.add(vo);
			} );
		}
		return voList;
	}

}
